#!/usr/bin/perl -I ./thirds -w
# Author: Micro Li (microli@sz.webex.com)
# Date: 05/18/2007
#
use strict;
use English;
use Data::Dumper;
use File::Iterator;
use CGI;
use Assistor;
use Cwd;
use JSON;

my $q = new CGI;
print "Content-type: application/json\n\n";

my $T_SNAPs  = $q->param("T_SNAPs");
my $assistor    = new Assistor();

my @arrT_SNAP = split(/,/, $T_SNAPs);
my $cnt = $#arrT_SNAP;


my $ret = {};
for (my $i = 0; $i <= $cnt; $i++) {
	$assistor->set_time_snap($arrT_SNAP[$i]);
	
	my $log_file    = $assistor->get_operate_log_file();
	my $file_content = "-1000";
	if (-e $log_file) {
		$file_content = $assistor->read_whole_file($log_file);
		my $tail512 = substr($file_content, -512);
	
		if (index($tail512, "<DONE></DONE>") != -1) {
			my $cmd = "mv $log_file $log_file" . "--";
			print `$cmd`;    
		}
	
		$file_content =~ s|\n|<br/>|g;
	}
	
	$ret->{$arrT_SNAP[$i]} = $file_content;
}

my $json = new JSON;

my $out = $json->pretty->encode($ret);
print $out;
